package com.mlnx.ui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.analysis.pojo.dto.dynamic.DynamicEcgAnalysisResult;
import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import com.mlnx.ui.dao.AbnormalCountMapper;
import com.mlnx.ui.dao.EcgReportMapper;
import com.mlnx.ui.dao.HeartRateMapper;
import com.mlnx.ui.entity.AbnormalCount;
import com.mlnx.ui.entity.EcgReport;
import com.mlnx.ui.entity.HeartRate;
import com.mlnx.ui.service.EcgReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class EcgReportServiceImpl extends BaseService<EcgReportMapper, EcgReport> implements EcgReportService {

    @Autowired
    @Override
    public void setBaseMapper(EcgReportMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private HeartRateMapper heartRateMapper;

    @Autowired
    private AbnormalCountMapper abnormalCountMapper;

    @Override
    @Transactional
    public void saveEcgReport(DynamicEcgAnalysisResult dynamicEcgAnalysisResult) {
        EcgReport ecgReport = new EcgReport();
        ecgReport.setCheckDate(new Date(dynamicEcgAnalysisResult.getStartTime()));
        ecgReport.setReportTime(new Date());
        ecgReport.setStartTime(new Date(dynamicEcgAnalysisResult.getStartTime()));
        ecgReport.setEndTime(new Date(dynamicEcgAnalysisResult.getEndTime()));
        ecgReport.setNumeration(dynamicEcgAnalysisResult.getReportNumeration());
        ecgReport.setPatientId(dynamicEcgAnalysisResult.getPatientId());
        ecgReport.setDiagnosisResult(dynamicEcgAnalysisResult.getDiagnosisResult());
        int total = (int) ((dynamicEcgAnalysisResult.getEndTime() - dynamicEcgAnalysisResult
                .getStartTime()) / 1000);
        ecgReport.setTotalTime(total);

        baseMapper.insert(ecgReport);

        HeartRate heartRate = new HeartRate();
        heartRate.setReportId(ecgReport.getId());
        heartRate.setAvgHeartRate(dynamicEcgAnalysisResult.getHeartStatistic()
                .getAvgHeart());
        heartRate.setDayRate(dynamicEcgAnalysisResult.getHeartStatistic()
                .getAvgDayHeart());
        heartRate.setNightRate(dynamicEcgAnalysisResult.getHeartStatistic()
                .getAvgNightHeart());
        heartRate.setMaxRate(dynamicEcgAnalysisResult.getHeartStatistic()
                .getMaxHeart());
        if (dynamicEcgAnalysisResult.getHeartStatistic().getMaxOccurTime() != 0) {
            heartRate.setMaxTime(new Date(dynamicEcgAnalysisResult
                    .getHeartStatistic().getMaxOccurTime()));
        }
        heartRate.setMinRate(dynamicEcgAnalysisResult.getHeartStatistic()
                .getMinHeart());
        if (dynamicEcgAnalysisResult.getHeartStatistic().getMinOccurTime() != 0) {
            heartRate.setMinTime(new Date(dynamicEcgAnalysisResult
                    .getHeartStatistic().getMinOccurTime()));
        }
        heartRate.setTotalHeart(dynamicEcgAnalysisResult.getHeartStatistic()
                .getSumHeart());
        heartRate.setTachycardia(dynamicEcgAnalysisResult.getEventStatistic()
                .getFastHeartCount());
        heartRate.setBradycardia(dynamicEcgAnalysisResult.getEventStatistic()
                .getSlowHeartCount());
        heartRate.setArrestTimes(dynamicEcgAnalysisResult.getEventStatistic()
                .getStopHeartCount());
        heartRate.setLongestArrest(dynamicEcgAnalysisResult.getEventStatistic().getMaxStopHeartTime());
        try {
            heartRateMapper.insert(heartRate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MutilDeviceServiceException(ResponseEnum.ServerError);
        }

        AbnormalCount abnormalCount = new AbnormalCount();
        abnormalCount.setReportId(ecgReport.getId());
        abnormalCount.setBigeminy(dynamicEcgAnalysisResult.getAbnormalStatistic()
                .getTwollCount());
        abnormalCount.setTrigeminy(dynamicEcgAnalysisResult.getAbnormalStatistic()
                .getThreellCount());
        abnormalCount.setFibrillationVf(dynamicEcgAnalysisResult
                .getAbnormalStatistic().getVfCount());
        abnormalCount.setMultipleBeats(dynamicEcgAnalysisResult
                .getAbnormalStatistic().getMostPbCount());
        abnormalCount.setOccasionalBeats(dynamicEcgAnalysisResult
                .getAbnormalStatistic().getAccidentalPbCount());
        abnormalCount.setPolymorphicPvc(dynamicEcgAnalysisResult
                .getAbnormalStatistic().getPvcCount());
        abnormalCount.setRont(dynamicEcgAnalysisResult.getAbnormalStatistic()
                .getRontCount());
        abnormalCount.setTwoVentricularBeats(dynamicEcgAnalysisResult.getAbnormalStatistic()
                .getTwoPbCount());
        abnormalCount.setVentricularTachycardia(dynamicEcgAnalysisResult
                .getAbnormalStatistic().getSxFastCount());
        abnormalCount.setSdnn(dynamicEcgAnalysisResult.getHeartRV().getSdnn());
        abnormalCount.setSdann(dynamicEcgAnalysisResult.getHeartRV().getSdann());
        abnormalCount.setRmssd(dynamicEcgAnalysisResult.getHeartRV().getRmssd());
        abnormalCount.setPnn50(dynamicEcgAnalysisResult.getHeartRV().getPnn50());
        try {
            abnormalCountMapper.insert(abnormalCount);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MutilDeviceServiceException(ResponseEnum.ServerError);
        }

    }

    @Override
    public Map<String, Object> getReportById(Integer id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        HeartRate heartRateDO = heartRateMapper.selectOne(new QueryWrapper<HeartRate>().eq("report_id", id));
        AbnormalCount abnormalCountDO = abnormalCountMapper.selectOne(new QueryWrapper<AbnormalCount>().eq("report_id", id));
        EcgReport ecgReportDO = getById(id);

        resultMap.put("ecgReportDO", ecgReportDO);
        resultMap.put("heartRateDO", heartRateDO);
        resultMap.put("abnormalCountDO", abnormalCountDO);

        return resultMap;
    }
}
